//
//  DiscoveryViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "DiscoveryViewController.h"
#import "ScanCodeViewController.h"
#import "HeaderWebViewController.h"
#import "ITHomePublicModel.h"
#import "UIImageView+WebCache.h"
@interface DiscoveryViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchControllerDelegate,UISearchResultsUpdating>
{
    NSTimer *_timer;
    UIImageView *_line;
    int num;
    BOOL upOrDown; //扫描条的上下状态。
}
@property (nonatomic, strong) NSMutableArray *searchArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *searchTableView;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UIImageView *image;
@end

@implementation DiscoveryViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getTableView];
    [self getNavigationView];
    self.view.backgroundColor = [UIColor grayColor];
    
    [self loadNavigationIndfo];
}

#pragma mark - 设置导航栏
- (void)loadNavigationIndfo
{
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    
    // 标题颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}


- (void)getTableView
{
    self.navigationController.navigationBar.translucent = YES;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

#pragma - mark - 设置搜索视图
- (void)getNavigationView
{
    //搜索按钮
//    self.navigationItem.leftBarButtonItem = nil;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(rightBarButtonAction)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    //搜索控制器
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.delegate = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.placeholder = @"请输入搜索内容";
    [self.navigationItem.titleView addSubview:self.searchController.searchBar];
    
    //搜索界面tableView
    self.searchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64 -49) style:UITableViewStylePlain];
    self.searchTableView.rowHeight = kScreenIp6(90);
    self.image.frame = CGRectMake(0, 10, kScreenWidth, 60);
    self.image.image = [UIImage imageNamed:@"atxt_no_result"];
    self.searchTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.01)];
    self.image.contentMode =  UIViewContentModeCenter;
    [self.searchTableView.tableHeaderView addSubview:_image];
    self.searchTableView.tableFooterView = [[UIView alloc] init];
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;
    [self.searchController.view addSubview:_searchTableView];

}


- (void)rightBarButtonAction
{
    [self presentViewController:self.searchController animated:YES completion:nil];
}
#pragma - mark - 搜索代理方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    [self.searchArray removeAllObjects];
    NSString *searchStr = [NSString stringWithFormat:@"http://rong.36kr.com/api/v1/news/search?keyword=%@&page=1&page_size=10",self.searchController.searchBar.text];
    NSString *UrlStr = [searchStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [LORequestManger GET:UrlStr success:^(id response) {
        NSDictionary *bDic = (NSDictionary *)response;
        NSDictionary *cDic = [bDic objectForKey:@"data"];
        NSArray *array = [cDic objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            ITHomePublicModel *model = [ITHomePublicModel itHomePubicModelWithDictionary:dic];
            [self.searchArray addObject:model];
        }
        _image.hidden = NO;
        [self.searchTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@",error);
        [ProgressHUD progressHUD:self.view labelText:@"加载失败"];
    }];
}

#pragma - mark - TableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([tableView isEqual:_tableView]) {
        return 2;
    }
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:_tableView]) {
        return 1;
    }
    return self.searchArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:_tableView]) {
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
                                 ];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        if (indexPath.section == 0) {
            cell.imageView.image = [UIImage imageNamed:@"tabbar_4_n"];
            cell.textLabel.text = @"扫扫看";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        
        return cell;
    }
    static NSString *cellIdentifier = @"cell1";
    [tableView registerClass:[ITHomePublicTableViewCell class] forCellReuseIdentifier:cellIdentifier];
    ITHomePublicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    ITHomePublicModel *model = self.searchArray[indexPath.row];
    _image.hidden = YES;
    [cell loadCellWithDataModel:model];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:_tableView]) {
        if (indexPath.section == 0 ) {
            ScanCodeViewController *scanVc = [[ScanCodeViewController alloc] init];
            [self presentViewController:scanVc animated:YES completion:nil];
        }
    }else{
        HeaderWebViewController *headerVc = [[HeaderWebViewController alloc] init];
        UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:headerVc];
        ITHomePublicModel *model = self.searchArray[indexPath.row];
        headerVc.urlString = [NSString stringWithFormat:@"http://rong.36kr.com/api/v1/news/%@",model.feed_id];
        [self presentViewController:nvc animated:YES completion:nil];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma - mark - 懒加载
- (NSMutableArray *)searchArray
{
    if (_searchArray == nil) {
        _searchArray = [NSMutableArray array];
    }
    return _searchArray;
}
- (UIImageView *)image
{
    if (_image == nil) {
        _image = [[UIImageView alloc] init];
    }
    return _image;
}


#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}
@end
